package cn.zh.stan.activity;

import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.util.MyScrollView;
import cn.zh.stan.util.ScrollListener;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class DtailGoods extends BaseActivity implements ScrollListener{

    ImageView dg_back;
    LinearLayout viewSpace;
    LinearLayout stickView;
    MyScrollView myScrollView;
    String goodsid,uid;
    TextView gdtxt,gdnum,gduint,gdprice,addcar;
    MyApplication mapp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dtail_goods);
        goodsid=getIntent().getStringExtra("goodsid");
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        getDtail();
        initv();

       }


    public void initv(){
        dg_back= (ImageView) findViewById(R.id.dg_back);
        dg_back.setOnClickListener(this);
        myScrollView = (MyScrollView) findViewById(R.id.scroll_view);
        myScrollView.registerListener(this);
        viewSpace = (LinearLayout) findViewById(R.id.stick_view_space);
        stickView = (LinearLayout) findViewById(R.id.stick_view);
        gdtxt= (TextView) findViewById(R.id.gdtxt);
        gdnum= (TextView) findViewById(R.id.gdnum);
        gduint= (TextView) findViewById(R.id.gduint);
        gdprice= (TextView) findViewById(R.id.gdprice);
        addcar= (TextView) findViewById(R.id.addcar);
        stickView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            //当布局文件加载完成时，会调用该方法
            @Override
            public void onGlobalLayout() {
                //初始化stickView的位置
                onScroll(0,0);
            }
        });
        addcar.setOnClickListener(this);

       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.dg_back:
                finish();
                break;
            case R.id.addcar:
                gaddCar();
                break;
              }
            }

    @Override
    public void onScroll(int top, int left) {
        stickView.setTranslationY(Math.max(top, viewSpace.getTop()));
     }


    //网络请求
    public  void getDtail(){
        OkHttpUtils.get(BaseUrl.goods_detail)
                .params("goodsId", goodsid)
                .params("userId", uid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                       System.out.println("--------------->商品详情==="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                              JSONObject Goods=js.getJSONObject("Goods");
                              String GoodsID = Goods.getString("GoodsID");
                              String GoodsNO = Goods.getString("GoodsNO");



                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                  });
                }


         //网络请求，加入购物车
    public void gaddCar(){
          OkHttpUtils.post(BaseUrl.goods_addcar)
                  .params("userId",uid)
                  .params("goodsId",goodsid)
                  .params("num", "1")
                  .execute(new StringCallback() {
                      @Override
                      public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                          System.out.println("----------------->添加商品到购物车==="+s);
                          try {
                              JSONObject js=new JSONObject(s);
                              boolean State=js.getBoolean("State");
                              String Msg=js.getString("Msg");
                              ToastUtil.showShort(DtailGoods.this,Msg);
                              if (State){
                                finish();
                              }
                          } catch (JSONException e) {

                          }
                      }
                      @Override
                      public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                          super.onError(isFromCache, call, response, e);
                      }
                  });
                 }




   }
